using System;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using Microsoft.Win32;

namespace SmsServerClient
{
	/// <summary>
	/// Summary description for Settings.
	/// </summary>
	public class Settings
	{
		private static NameValueCollection m_settings;
		private static string m_settingsPath;
        private const Int16 DefaultTcpPort = 30451;
		static Settings()
		{
            m_settingsPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            m_settingsPath += @"\Settings.xml";
			
			if(!File.Exists(m_settingsPath))
				throw new FileNotFoundException(m_settingsPath + " could not be found.");

			System.Xml.XmlDocument xdoc = new XmlDocument();
			xdoc.Load(m_settingsPath);
			XmlElement root = xdoc.DocumentElement;
			System.Xml.XmlNodeList nodeList = root.ChildNodes.Item(0).ChildNodes;

			// Add settings to the NameValueCollection.
			m_settings = new NameValueCollection();
            m_settings.Add("DeleteInbox", nodeList.Item(0).Attributes["value"].Value);
            m_settings.Add("AdminNumber", nodeList.Item(1).Attributes["value"].Value);
            m_settings.Add("InformAdmin", nodeList.Item(2).Attributes["value"].Value);
            m_settings.Add("BlockNonNumberic", nodeList.Item(3).Attributes["value"].Value);
			m_settings.Add("InformClient", nodeList.Item(4).Attributes["value"].Value);
            m_settings.Add("TcpPort", nodeList.Item(5).Attributes["value"].Value);
        }

		public static void Update()
		{
			XmlTextWriter tw = new XmlTextWriter(m_settingsPath, System.Text.UTF8Encoding.UTF8);
			tw.WriteStartDocument();
			tw.WriteStartElement("configuration");
			tw.WriteStartElement("appSettings");

			for(int i=0; i<m_settings.Count; ++i)
			{
				tw.WriteStartElement("add");
				tw.WriteStartAttribute("key", string.Empty);
				tw.WriteRaw(m_settings.GetKey(i));
				tw.WriteEndAttribute();

				tw.WriteStartAttribute("value", string.Empty);
				tw.WriteRaw(m_settings.Get(i));
				tw.WriteEndAttribute();
				tw.WriteEndElement();
			}

			tw.WriteEndElement();
			tw.WriteEndElement();

			tw.Close();
		}

        public static string DeleteInbox
		{
            get { return m_settings.Get("DeleteInbox"); }
            set { m_settings.Set("DeleteInbox", value); }
		}

        public static string AdminNumber
		{
            get { return m_settings.Get("AdminNumber"); }
            set { m_settings.Set("AdminNumber", value); }
		}

        public static string InformAdmin
		{
            get { return m_settings.Get("InformAdmin"); }
            set { m_settings.Set("InformAdmin", value); }
		}

        public static string BlockNonNumberic
		{
            get { return m_settings.Get("BlockNonNumberic"); }
            set { m_settings.Set("BlockNonNumberic", value); }
		}

        public static string InformClient
		{
            get { return m_settings.Get("InformClient"); }
            set { m_settings.Set("InformClient", value); }
		}
        public static Int16 TcpPort
        {
            get
            {
                try { return Convert.ToInt16(m_settings.Get("TcpPort")); }
                catch {}
                return DefaultTcpPort;
            }
            set 
            { 
                m_settings.Set("TcpPort", value.ToString());
                RegistryKey regKey = Registry.LocalMachine.CreateSubKey("\\Software\\MSRI");
                regKey.SetValue("TcpPort", value.ToString());
            }
        }
	}
}
